package com.jboost.modules.app.service.strategry.gc;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.JsonObject;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.BaseResult;
import com.jboost.modules.app.dto.GcKcDto;
import com.jboost.modules.app.dto.HsMatrierDto;
import com.jboost.modules.app.dto.XMUserReqDto;
import com.jboost.modules.app.dto.resp.GcKcResp;
import com.jboost.modules.app.dto.resp.GcOrderResp;
import com.jboost.modules.app.dto.resp.GcProductResp;
import com.jboost.modules.system.entity.AJdySyncData;
import com.jboost.modules.system.entity.HsMaterialNeed;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

@Service(value = "HsGcKcQuery")
@Slf4j
public class HsGcKcQuery extends AbstractGcService<JsonObject, Boolean>  {

    private final static String KC_API = url_prefix_test + "/inventory/get_product_inventory";



    public String getFormId() {
        return "646eccc96818d90007de2b2a";
    }

    public static void main(String[] args) throws IOException {
    }

    @Scheduled(cron = "0 0/10 * * * ?")
    public BaseResult<Boolean> invokeStrategy() throws Exception {
        log.info("开始批量查询gc库存数据");
        List<GcKcResp.DataBean> list = list(KC_API, gcHeaders, GcKcResp.class);
        for (GcKcResp.DataBean e : list) {
//            if(!pass(e.getProduct_sku(),e)){
//                continue;
//            }
            GcKcDto.Insert.BarnStockInsertBean.BarnStockInsertBeanBuilder barnStockInsertBeanBuilder = GcKcDto.Insert.BarnStockInsertBean.builder()
                    .stocking(getParaMap(e.getStocking()))
                    .onway(getParaMap(e.getOnway()))
                    .pi_freeze(getParaMap(e.getPi_freeze()))
                    .product_barcode(getParaMap(e.getProduct_barcode()))
                    .sellable(getParaMap(e.getSellable()))
                    .unsellable(getParaMap(e.getUnsellable()))
                    .warehouse_desc(getParaMap(e.getWarehouse_desc()))
                    .shipped(getParaMap(e.getShipped()))
                    .reserved(getParaMap(e.getReserved()));
            HashMap<String, Object> param = Maps.newHashMap();
            param.put("page",1);
            param.put("pageSize",20);
            param.put("product_sku", e.getProduct_sku());
            try {
                String post = HttpRestUtils.post(PRODUCT_INFO, param, gcHeaders);
                GcProductResp gcProductResp = JSONUtil.fromJson(post, GcProductResp.class);
                List<GcProductResp.DataBean> data = gcProductResp.getData();
                data.forEach(e1 -> {
                    barnStockInsertBeanBuilder.product_status(getParaMap(e1.getProduct_status()))
                            .product_title_cn(getParaMap(e1.getProduct_title_cn()));

                });
            } catch (IOException ex) {
                log.warn("查询商品信息错误", e);
            }
            AJdySyncData aJdySyncData = gcDataMapper.selectById(e.getProduct_barcode()+e.getWarehouse_code());
            GcKcDto.Insert insert = new GcKcDto.Insert();
            insert.setBarn_stock(getParaMap(Arrays.asList(barnStockInsertBeanBuilder.build())));
            if (Objects.isNull(aJdySyncData)) {
                try {
                    String s = jdyCreate(insert);
                    HashMap map = JSONUtil.fromJson(s, HashMap.class);
                    String dataId = ((JSONObject) map.get("data")).getString("_id");
                    save(e.getProduct_barcode()+e.getWarehouse_code(), JSONUtil.toJson(e), dataId);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }else {
                jdyUpdate(aJdySyncData.getDataType(),insert);
            }
        }
        ;

        return BaseResult.success();
    }

}
